define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {
        var change = 0;
    //删除后刷新菜单角标
        $('.btn-del').click(function(){
            change = 1;
        })
    //审核后刷新菜单角标
        $('.btn-ajax').click(function(){
            change = 1;
        })
    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'collision/index' + location.search,
                    /*add_url: 'collision/add',
                    edit_url: 'collision/edit',*/
                    del_url: 'collision/del',
                    multi_url: 'collision/multi',
                    import_url: 'collision/import',
                    table: 'collision',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                fixedColumns: true,
                fixedRightNumber: 1,
                search:false,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'connection_id', title: __('Connection_id'),formatter: Controller.api.formatter.connection},
                        {field: 'connection_name', title: __('Connection_name'), operate: 'LIKE',cellStyle: function (value, row, index) {
                                    return {css: {"color": "red"}};
                            }},
                        {field: 'connection_mobile', title: __('Connection_mobile'), operate: 'LIKE',formatter: function (row,index) {
                            return index.connection_mobile_see
                        },cellStyle: function (value, row, index) {
                                    return {css: {"color": "red"}};
                            }},
                        {field: 'connection_mobile_two', title: __('Connection_mobile_two'), operate: 'LIKE',formatter: function (row,index) {
                            return index.connection_mobile_two_see
                        },cellStyle: function (value, row, index) {
                                    return {css: {"color": "red"}};
                            }},
                        {field: 'canal_name',title: __('Canal_name'), operate: 'LIKE'},
                        //{field: 'connection_mobile_see', title: __('Connection_mobile_see'), operate: 'LIKE'},
                        {field: 'bind_staff_ibec_name', title: __('Bind_staff_ibec_name'), operate: 'LIKE'},
                        {field: 'bind_staff_name', title: __('Bind_staff_name'), operate: 'LIKE'},
                        {field: 'bind_staff_mobile', title: __('Bind_staff_mobile'), operate: 'LIKE'},
                        {field: 'staff_ibec_name', title: __('Staff_ibec_name'), operate: 'LIKE'},
                        {field: 'staff_name', title: __('Staff_name'), operate: 'LIKE',cellStyle: function (value, row, index) {
                                    return {css: {"color": "red"}};
                            }},
                        {field: 'staff_mobile', title: __('Staff_mobile'), operate: 'LIKE',cellStyle: function (value, row, index) {
                                    return {css: {"color": "red"}};
                            }},
                        {field: 'staff_connection_id', title: __('Staff_connection_id'),operate:false,formatter: Controller.api.formatter.staff_connection},
                        {field: 'status', title: __('Status'), searchList: {"0":__('Status 0'),"1":__('Status 1')}, formatter: Table.api.formatter.status},
                        {field: 'handle_time', title: __('Handle_time'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'operate', title: __('Operate'), table: table,
                            buttons: [
                                {
                                    name: 'see',
                                    title: __('详情'),
                                    classname: 'btn btn-xs btn-success btn-dialog',
                                    text: '详情',
                                    //icon: 'fa fa-folder-o',
                                    url: function(row,column){
                                        var url='collision/see?id='+row.id;
                                        return url;
                                    },
                                },
                                {
                                    name: 'ok',
                                    title:'已处理',
                                    text: '已处理',
                                    classname: 'btn btn-xs btn-success btn-magic btn-ajax',
                                    icon: '',
                                    confirm: '确定改为已处理？',
                                    visible: function (row) {
                                        // console.log(row)
                                        //返回true时按钮显示,返回false隐藏
                                        if (row.status=='1' ) {
                                            return false
                                        }
                                        return true;

                                    },
                                    url: function (row, column) {
                                        var url = 'collision/ok?ids=' + row.id+'&status=1';
                                        return url;
                                    },
                                    success: function (data, ret) {
                                        table.bootstrapTable('refresh', {});
                                        return false;
                                    },
                                    error: function (data, ret) {
                                        table.bootstrapTable('refresh', {});
                                        return false;
                                    },
                                },
                            ],
                            events: Table.api.events.operate, formatter: Table.api.formatter.operate
                        }
                    ]
                ],
                onLoadSuccess: function (data) {
                    // 在表格第次加载成功后,刷新左侧菜单栏彩色小角标,支持一次渲染多个
                    // 如果需要在进入后台即显示左侧的彩色小角标,请使用服务端渲染方式,详情修改application/admin/controller/Index.php
                    top.window.Backend.api.sidebar({
                        'collision': data.untreatedTotal
                    });
                    if(change == 1){
                        top.window.Backend.api.refreshmenu(); //刷新菜单
                        change = 0;
                    }
                    //Toastr.info("左侧角标已经刷新成功");
                }
            });
            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevient($("form[role=form]"));
            },formatter: {
                connection: function (value, row, index) {
                    //这里手动构造URL
                    url = "connection?id=" + row.connection_id;
                    this.url = url;
                    return '<a href="' + url + '" class=" addtabsit" title="' + __("Search %s", row.connection_name+"客户") + '">' + __('%s', value) + '</a>';
                    //return Table.api.formatter.addtabs.call(this, value, row, index);
                },
                staff_connection: function (value, row, index) {
                    //这里手动构造URL
                    url = "connection?id=" + row.staff_connection_id;
                    this.url = url;
                    return '<a href="' + url + '" class=" addtabsit" title="' + __("Search %s", row.staff_connection_id+"客户") + '">' + __('%s', value) + '</a>';
                    //return Table.api.formatter.addtabs.call(this, value, row, index);
                }
            }
        }
    };
    return Controller;
});
